package com.jackrain.nea.omslocal.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.config.PropertiesConf;
import com.jackrain.nea.omslocal.conf.LoadCloumnsConfig;
import com.jackrain.nea.omslocal.conf.LoadSellerNickConfig;
import com.jackrain.nea.omslocal.util.SplitArrUtil;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.web.query.QueryEngine;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @author ：wcy
 * @date ：Created in 11:28 2018/8/21
 * description ：
 */
@Component
@Slf4j
public class QiMenEventProduceService {
    private final SendMqServies sendMq;
    private final PropertiesConf pconf;
    private final ChangeOrdStatusService changeOrd;

    @Autowired
    public QiMenEventProduceService(SendMqServies sendMq, PropertiesConf pconf, ChangeOrdStatusService changeOrd) {
        this.sendMq = sendMq;
        this.pconf = pconf;
        this.changeOrd = changeOrd;
    }


    /**
     * 淘宝订单全渠道回流
     */
    public void ordStart() {
        // 获取自身 bean
        QiMenEventProduceService qiMenEventProduceService = ApplicationContextHandle.getBean(QiMenEventProduceService.class);
        qiMenEventProduceService.queryOrdStatus();
    }

    /**
     * 循环遍历查询订单状态表,以消息队列同步至线上
     *
     */
    @Transactional(rollbackFor = Exception.class)
    public void queryOrdStatus(){

        try {
            // 需要查询的字段    t_omsorderlink
            JSONArray sellerNickArr = (JSONArray) LoadSellerNickConfig.sellerNickInfoMap.get("sellerNickArr");
            if (sellerNickArr == null || sellerNickArr.isEmpty()){
                log.debug("买家昵称列表为空");
                return;
            }
            for (int i = 0; i < sellerNickArr.size(); i++) {
                JSONObject sellerNickObj = sellerNickArr.getJSONObject(i);
                String sellerNick = sellerNickObj.getString("SELLERNICK");
                String files = (String) LoadCloumnsConfig.ordstatusmap.get(pconf.getProperty("ons.ordstatustable"));
                int rownums = pconf.getPropertyInt("ons.ordnum");
                // 查询订单状态表
                String countSql = "select " + files + " from T_OMSORDERLINK where syncstatus = 0 and nick = ? and platform = 'MAIN' " +
                        "and rownum <= " + rownums + " order by id for update skip locked";

                JSONArray jsonArr = QueryEngine.getInstance().doQueryObjectArray(countSql,new Object[]{sellerNick});

                if (jsonArr == null || jsonArr.isEmpty()) {
                    log.debug(sellerNick+"ordstatusArr is null");
                    continue;
                }
                JSONObject transeObj = new JSONObject();
                JSONObject chekObj = new JSONObject();
                JSONObject notifyObj = new JSONObject();
                JSONObject outObj = new JSONObject();

                transeObj.put("status", pconf.getProperty("ons.ordstatus1"));
                chekObj.put("status", pconf.getProperty("ons.ordstatus2"));
                notifyObj.put("status", pconf.getProperty("ons.ordstatus3"));
                outObj.put("status", pconf.getProperty("ons.ordstatus4"));

                JSONArray traseArr = new JSONArray();
                JSONArray chekArr = new JSONArray();
                JSONArray notifyArr = new JSONArray();
                JSONArray outArr = new JSONArray();

                SplitArrUtil splitArrUtil = ApplicationContextHandle.getBean(SplitArrUtil.class);
                // 循环遍历,数据分组
                if (jsonArr.size()>200){
                    JSONArray jsonArray = splitArrUtil.splitArr(jsonArr, 200);
                    for (Object o : jsonArray) {
                        JSONArray sendArr = JSONArray.parseArray(o.toString());
                        for (Object obj : sendArr) {
                            JSONObject tempObj = JSONObject.parseObject(obj.toString());
                            System.out.println(tempObj.toJSONString());
                            if (tempObj.getString("STATUS").equals(pconf.getProperty("ons.ordstatus1"))) {
                                String tid = tempObj.getString("TID");
                                tempObj.fluentClear();
                                tempObj.put("tid", tid);
                                traseArr.add(tempObj);

                            } else if (tempObj.getString("STATUS").equals(pconf.getProperty("ons.ordstatus2"))) {
                                String tid = tempObj.getString("TID");
                                tempObj.fluentClear();
                                tempObj.put("tid", tid);
                                chekArr.add(tempObj);

                            } else if (tempObj.getString("STATUS").equals(pconf.getProperty("ons.ordstatus3"))) {
                                String tid = tempObj.getString("TID");
                                tempObj.fluentClear();
                                tempObj.put("tid", tid);
                                notifyArr.add(tempObj);

                            } else if (tempObj.getString("STATUS").equals(pconf.getProperty("ons.ordstatus4"))) {
                                String tid = tempObj.getString("TID");
                                tempObj.fluentClear();
                                tempObj.put("tid", tid);
                                outArr.add(tempObj);
                            }
                        }
                        log.debug("trans : "+traseArr.size());
                        log.debug("chek :"+chekArr.size());
                        log.debug("notify :"+notifyArr.size());
                        log.debug("out :"+outArr.size());
                        transeObj.put("bills", traseArr);
                        chekObj.put("bills", chekArr);
                        notifyObj.put("bills", notifyArr);
                        outObj.put("bills", outArr);

                        JSONArray dataArr = new JSONArray();
                        JSONObject jsonObject = new JSONObject();
                        dataArr.add(transeObj);
                        dataArr.add(chekObj);
                        dataArr.add(notifyObj);
                        dataArr.add(outObj);
                        jsonObject.put("data", dataArr);
                        jsonObject.put("nick",sellerNick);

                        // 有需要同步的订单数据 将查询到的数据以消息形式发送
                        String tag = pconf.getProperty("ons.comp");
                        String topic = pconf.getProperty("ons.qiMenEventTopic", "TAOBAO_EVENTPRO");
                        sendMq.sendMqinfoObj(jsonObject, topic, tag);
                        System.out.println("发送的消息体===>"+jsonObject.toJSONString());
                        //// 更改库存状态
                        changeOrd.changeStatus(jsonArr);
                    }
                }else {
                    for (Object obj : jsonArr) {
                        JSONObject tempObj = JSONObject.parseObject(obj.toString());
                        System.out.println(tempObj.toJSONString());
                        if (tempObj.getString("STATUS").equals(pconf.getProperty("ons.ordstatus1"))) {
                            String tid = tempObj.getString("TID");
                            tempObj.fluentClear();
                            tempObj.put("tid", tid);
                            traseArr.add(tempObj);

                        } else if (tempObj.getString("STATUS").equals(pconf.getProperty("ons.ordstatus2"))) {
                            String tid = tempObj.getString("TID");
                            tempObj.fluentClear();
                            tempObj.put("tid", tid);
                            chekArr.add(tempObj);

                        } else if (tempObj.getString("STATUS").equals(pconf.getProperty("ons.ordstatus3"))) {
                            String tid = tempObj.getString("TID");
                            tempObj.fluentClear();
                            tempObj.put("tid", tid);
                            notifyArr.add(tempObj);

                        } else if (tempObj.getString("STATUS").equals(pconf.getProperty("ons.ordstatus4"))) {
                            String tid = tempObj.getString("TID");
                            tempObj.fluentClear();
                            tempObj.put("tid", tid);
                            outArr.add(tempObj);
                        }
                    }
                    log.debug("trans : "+traseArr.size());
                    log.debug("chek :"+chekArr.size());
                    log.debug("notify :"+notifyArr.size());
                    log.debug("out :"+outArr.size());
                    transeObj.put("bills", traseArr);
                    chekObj.put("bills", chekArr);
                    notifyObj.put("bills", notifyArr);
                    outObj.put("bills", outArr);

                    JSONArray dataArr = new JSONArray();
                    JSONObject jsonObject = new JSONObject();
                    dataArr.add(transeObj);
                    dataArr.add(chekObj);
                    dataArr.add(notifyObj);
                    dataArr.add(outObj);
                    jsonObject.put("data", dataArr);
                    jsonObject.put("nick",sellerNick);

                    // 有需要同步的订单数据 将查询到的数据以消息形式发送
                    String tag = pconf.getProperty("ons.comp");
                    String topic = pconf.getProperty("ons.qiMenEventTopic", "TAOBAO_EVENTPRO");
                    sendMq.sendMqinfoObj(jsonObject, topic, tag);
                    System.out.println("发送的消息体===>"+jsonObject.toJSONString());
                    //// 更改库存状态
                    changeOrd.changeStatus(jsonArr);
                }

            }
        } catch (Exception e) {

            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            // 将错误以流写出
            e.printStackTrace(pw);
            System.out.println(sw.toString());
        }


    }
}
